import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { isLogin } from './utils/auth'
import loadMenuCompleted from './utils/loadMenu'
import store from '@/store'

const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (isLogin()) {
    if (!store.getters['menu/getMenuRoutes']) {
      loadMenuCompleted().then(resp => {
        resp && next()
      })
    } else next()
  } else {
    if (whiteList.includes(to.path)) {
        next()
    } else {
        next('/login')
    }
  }
})

router.afterEach((to, from) => {
  document.title = (to.meta && to.meta.title) || '工程经营'
  NProgress.done() // 结束Progress
})
